<template>
  <div class="register">
    <el-form
      :model="registerForm"
      :rules="rules"
      label-width="100px"
      ref="registerForm"
      class="form"
    >
      <h1 class="title">注册</h1>
      <el-form-item label="用户" prop="username">
        <el-input
          v-model="registerForm.username"
          placeholder="请输入用户名"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          v-model="registerForm.password"
          type="password"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="comfirmPassword">
        <el-input
          v-model="registerForm.comfirmPassword"
          type="password"
          placeholder="再次确认密码"
        ></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input
          v-model="registerForm.email"
          type="email"
          placeholder="请输入邮箱"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button class="but" @click="cancle">取消</el-button>
        <el-button
          class="but"
          type="primary"
          @click="submitForm('registerForm')"
          >注册</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { register } from "../api/login";
import { ElMessage } from "element-plus";
export default {
  name: "Login",
  data() {
    return {
      registerForm: {
        username: "",
        password: "",
        comfirmPassword: "",
        email: "",
      },
      rules: {
        username: [
          { message: "请输入用户名称", trigger: "blur" },
          {
            min: 3,
            max: 15,
            message: "长度在 3 到 15 个字符",
            trigger: "blur",
          },
        ],
        password: [{ message: "请输入密码", trigger: "blur" }],
        emial: [
          {
            type: "email",
            message: "请输入正确的邮箱",
            trigger: ["blur"],
          },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      console.log(formName);
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.registerForm.password == this.registerForm.comfirmPassword) {
            console.log(this.registerForm);
            const data = {
              email: this.registerForm.email,
              password: this.registerForm.password,
              status: 0,
              username: this.registerForm.username,
            };
            register(data).then((res) => {
              if (res.code == 200) {
                ElMessage.success("跳转回登录页面");
                this.$router.push("/login");
              } else {
                ElMessage.error(res.msg);
              }
            });
          }
        } else {
          ElMessage.error("请输入正确的格式");
          return false;
        }
      });
    },
    cancle() {
      this.$router.back(-1);
    },
  },
};
</script>
<style lang="less" scoped>
.register {
  width: 100%;
  height: 100vh;
  background: url(../assets/login-bg.png) no-repeat fixed center !important;
  background-size: 100% 100% !important; /**拉伸图片 */
  .form {
    width: 400px;
    text-align: center;
    margin: 0 auto;
    padding: 1%;
    background: #ffffff;
    position: relative;
    top: 25%;
    border-radius: 10px;
    .title {
      font-size: 20px;
    }
    .but {
      margin: 0 auto;
    }
  }
}
</style>
